package br.com.emmis.tercons.controller;

import android.media.MediaRecorder;
import android.os.Environment;

import java.io.File;
import java.io.IOException;

import br.com.emmis.tercons.model.AudioSettings;

/**
 * Created by vaner on 07/10/2014.
 */
public class TercoRecorder {

    private String mAudioName;
    private MediaRecorder mRecorder;

    public TercoRecorder(int prayId) {

        if (Environment.getDataDirectory().getFreeSpace() > 1000000000) {
            mAudioName = Environment.getDataDirectory().getAbsolutePath() + File.separator + "emmis" + File.separator + "tercons" + File.separator + AudioSettings.getAudioFileName(prayId);
        }
        else if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            mAudioName = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "emmis" + File.separator + "tercons" + File.separator + AudioSettings.getAudioFileName(prayId);
        }

        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mRecorder.setOutputFile(mAudioName);
        try {
            mRecorder.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void startRecord() {
        mRecorder.start();
    }

    public void stopRecord() {
        mRecorder.stop();
    }

}
